//
//  DailySentenceViewModel.swift
//  FreeWallpaperSwiftUI
//
//  Created by 张少康 on 2023/12/4.
//

import Foundation
import Alamofire

class DailySentenceViewModel: ObservableObject {
    
    private let positiveWords = [
        "Love",
        "Courage",
        "Hope",
        "Strength",
        "Gratitude",
        "Kindness",
        "Happiness",
        "Forgiveness",
        "Effort",
        "Growth",
        "Goodness",
        "Dream",
        "Appreciation",
        "Sunshine",
        "Smile",
        "Understanding",
        "Inspiration",
        "Blessing",
        "Friendship",
        "Diligence"
    ]
    
    @Published var positiveWord: String  = ""
    
    @Published var text: String = ""
    
    @Published var imageUrl: String = ""
        
    init() {
        
        self.positiveWord = positiveWords.randomElement() ?? "Love"
        
        let index = [0,1,2].randomElement()
        switch index {
        case 0:
            getShanbayPositiveSentence()
        case 1:
            getOnePositiveSentence()
        default:
            getCibaPositiveSentence()
        }
        
    }

    func getShanbayPositiveSentence() {
        Task {
            do {
                
                let data = try await AF.request("https://apiv3.shanbay.com/weapps/dailyquote/quote/").serializingDecodable(ShandaySentenceModel.self).value
                DispatchQueue.main.async {
                    self.text = data.content ?? ""
                    self.imageUrl = data.origin_img_urls?.first ?? ""
                }
                
            }catch {
                print("网络请求出错")
            }
        }
    }
    
    func getOnePositiveSentence() {
        Task {
            do {
                
                let data = try await AF.request("http://v3.wufazhuce.com:8000/api/channel/one/0/0").serializingDecodable(OneSentenceModel.self).value
                if let firstModel = data.data?.content_list?.first {
                    DispatchQueue.main.async {
                        self.text = firstModel.forward ?? ""
                        self.imageUrl = firstModel.img_url ?? ""
                    }
                }
                
            }catch {
                print("网络请求出错: \(error)")
            }
        }
    }
    
    func getCibaPositiveSentence() {
        Task {
            do {
                
                let data = try await AF.request("https://open.iciba.com/dsapi/").serializingDecodable(CibaSentenceModel.self).value
                DispatchQueue.main.async {
                    self.text = data.note ?? ""
                    self.imageUrl = data.picture2 ?? ""
                }
                
            }catch {
                print("网络请求出错")
            }
        }
    }
    
    func changeText() {
        Task {
            do {
                
                let data = try await AF.request("https://v1.hitokoto.cn/?c=j").serializingDecodable(TextHitokotoMode.self).value
                DispatchQueue.main.async {
                    self.text = data.hitokoto ?? ""
                }
                
            }catch {
                print("网络请求出错")
            }
        }
    }
}

//MARK: 更换文字  使用 一言 [ https://developer.hitokoto.cn/sentence/demo.html ]
struct TextHitokotoMode: Codable {
    let id: Int?
    let uuid: String?
    let hitokoto: String?
    let type: String?
    let from: String?
    let from_who: String?
    let creator: String?
    let creator_uid: Int?
    let reviewer: Int?
    let commit_from: String?
    let created_at: String?
    let length: Int?
}

//MARK:  扇贝单词 的model
/// url: https://apiv3.shanbay.com/weapps/dailyquote/quote/
struct ShandaySentenceModel: Codable {
    var id: String?
    var content: String?
    var author: String?
    var assign_date: String?
    var ad_url: String?
    var daily_audio_urls: [String]?
    var translation: String?
    var join_num: Int?
    var share_img_urls: [String]?
    var poster_img_urls: [String]?
    var origin_img_urls: [String]?
    var share_urls: [String: String]?
    var track_object: TrackObject?
    var share_url: String?
}

struct TrackObject: Codable {
    var code: String?
    var share_url: String?
    var object_id: Int?
}


// MARK: -- ONE 每日一句
struct OneSentenceModel: Codable {
    let res: Int?
    let data: DataInfo?
}

struct Icons: Codable {
    let day: String?
    let night: String?
}

struct Weather: Codable {
    let city_name: String?
    let date: String?
    let temperature: String?
    let humidity: String?
    let climate: String?
    let wind_direction: String?
    let hurricane: String?
    let icons: Icons?
}

struct TextAuthorInfo: Codable {
    let text_authorName: String?
    let text_authorWork: String?
    let text_authorDesc: String?
}

struct ShareInfo: Codable {
    let url: String?
    let image: String?
    let title: String?
    let content: String?
}

struct ShareList: Codable {
    let wx: ShareInfo?
    let wx_timeline: ShareInfo?
    let weibo: ShareInfo?
    let qq: ShareInfo?
}

struct ContentList: Codable {
    let id: String?
    let category: String?
    let display_category: Int?
    let item_id: String?
    let title: String?
    let forward: String?
    let img_url: String?
    let like_count: Int?
    let post_date: String?
    let last_updateDate: String?
    let author: [String: String]?
    let video_url: String?
    let audio_url: String?
    let audio_platform: Int?
    let start_video: String?
    let has_reading: Int?
//    let volume: AnyObject?
    let pic_info: String?
    let words_info: String?
    let text_authorInfo: TextAuthorInfo?
    let subtitle: String?
    let number: Int?
    let serial_id: Int?
    let serial_list: [String]?
    let movie_story_dd: Int?
    let ad_id: Int?
    let ad_type: Int?
    let ad_pvurl: String?
    let ad_linkurl: String?
    let ad_makettime: String?
    let ad_closetime: String?
    let ad_shareCnt: String?
    let ad_pvurlVendor: String?
    let content_id: String?
    let content_type: String?
    let content_bgcolor: String?
    let share_url: String?
    let share_info: ShareInfo?
    let share_list: ShareList?
    let tag_list: [String]?
    let orientation: String?
}

struct MenuList: Codable {
    let vol: String?
    let list: [MenuContent]?
}

struct MenuContent: Codable {
    let content_type: String?
    let content_id: String?
    let title: String?
}

struct DataInfo: Codable {
    let id: String?
    let weather: Weather?
    let date: String?
    let content_list: [ContentList]?
    let menu: MenuList?
    let ad: [String]?
}

// MARK: -- 金山词霸
struct CibaSentenceModel: Codable {
    var sid: String?
    var tts: String?
    var content: String?
    var note: String?
    var love: String?
    var translation: String?
    var picture: String?
    var picture2: String?
    var caption: String?
    var dateline: String?
    var s_pv: String?
    var sp_pv: String?
    var fenxiang_img: String?
    var picture3: String?
    var picture4: String?
    var tags: [String]?
}

